﻿from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.gui import *
import qgis

from dialog_region import Dialog_region

# initialize Qt resources from file resouces.py
import resources_rc
import sys, os

class TestPlugin():

  def __init__(self, iface):
    # save reference to the QGIS interface
    self.iface = iface
    self.canvas = self.iface.mapCanvas()

  def initGui(self):

    # create action that will start plugin configuration
    self.select_region = QAction(QIcon(":/plugins/testPlugin/icon.png"), "Select a study region", self.iface.mainWindow())
    self.select_fault = QAction(QIcon(":/plugins/testPlugin/icon.png"), "Select faults", self.iface.mainWindow())
    #self.select_fault.setWhatsThis("Configuration for test plugin")
    #self.action.setStatusTip("This is status tip")
    QObject.connect(self.select_region, SIGNAL("triggered()"), self.run)
    QObject.connect(self.select_fault, SIGNAL("triggered()"), self.run)

    # add toolbar button and menu item
    self.iface.addToolBarIcon(self.select_region)
    self.iface.addPluginToWebMenu("&F_hazard", self.select_region)
    self.iface.addPluginToWebMenu("&F_hazard", self.select_fault)



    # connect to signal renderComplete which is emitted when canvas rendering is done
    #QObject.connect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)
    #QObject.connect(self.iface.mapCanvas(), SIGNAL("xyCoordinates(const QgsPoint &)"), self.listen_xyCoordinates)
  def unload(self):
    # remove the plugin menu item and icon
    self.iface.removePluginMenu("&Test plugins",self.select_region)
    self.iface.removeToolBarIcon(self.select_region)

    # disconnect form signal of the canvas
    QObject.disconnect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)

  def run(self):
    # create and show a configuration dialog or something similar
    #QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed F7")

    self.dlg = Dialog_region(self.iface.mainWindow())
    QObject.connect(self.dlg.ui.selectButton, SIGNAL("clicked()"), self.selectRegionClick)
    QObject.connect(self.dlg.ui.cancelButton, SIGNAL("clicked()"), self.cancelRegionClick)
    self.dlg.show()

    #self.addlayer()

  def renderTest(self, painter):
    # use painter for drawing to map canvas
    print "TestPlugin: renderTest called!"



  def selectRegionClick(self):
    self.dlg.hide()
    self.removeLayer()
    self.addlayer(self.dlg.ui.comboBox.currentIndex())
    #QMessageBox.information(self.iface.mainWindow(),"Ok",self.dlg.ui.comboBox.currentText ())


  def cancelRegionClick(self):
    self.dlg.hide()

  def addlayer(self,choice):

    self.canvas.freeze(False)
    #add layer
    provinceFileName = "map/provinces1.shp"
    countryFileName = "map/quocgia.shp"
    faultFileName = "map/faults.shp"

    provincelayer = QgsVectorLayer(os.path.abspath(provinceFileName), provinceFileName[provinceFileName.rfind("/")+1:-4], "ogr")
    countryLayer = QgsVectorLayer(os.path.abspath(countryFileName), countryFileName[countryFileName.rfind("/")+1:-4], "ogr")
    faultLayer = QgsVectorLayer(os.path.abspath(faultFileName), faultFileName[faultFileName.rfind("/")+1:-4], "ogr")

    if not provincelayer.isValid() or not countryLayer.isValid() or not faultLayer.isValid():
        QMessageBox.information(self.iface.mainWindow(),"Ok","Layer failed to load! :(")
        return
    else:
        QgsMapLayerRegistry.instance().addMapLayer(countryLayer);
        QgsMapLayerRegistry.instance().addMapLayer(provincelayer);
        QgsMapLayerRegistry.instance().addMapLayer(faultLayer);

        ##self.iface.mapCanvas().emit(SIGNAL("xyCoordinates(const QgsPoint &)"), QgsPoint(500,45))
        self.canvas.zoomScale(3016999)

        if choice==0:
         # pan to North
         self.canvas.panActionEnd(QPoint(100,680))
        elif choice==2:
        #pan to South
            self.canvas.panActionEnd(QPoint(100,-500))
        self.canvas.freeze(True)

  def removeLayer(self):
        allLayers = self.canvas.layers()
        for i in allLayers:
            QgsMapLayerRegistry.instance().removeMapLayer(i.getLayerID());
